package com.typhoon.spring_jdbctemplate.util;

import java.lang.reflect.Field;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Comparator;
import java.util.List;
import java.util.TreeMap;

import org.springframework.jdbc.core.BatchPreparedStatementSetter;

public class BeanBatchPreparedStatementSetter <T,C extends List<T>> implements BatchPreparedStatementSetter {
	
	private Class<T> initClass;
	
	private C c;

	public BeanBatchPreparedStatementSetter(Class<T> initClass,C c) {
		super();
		this.initClass = initClass;
		this.c = c;
	}

	@Override
	public void setValues(PreparedStatement ps, int i) throws SQLException {
		T obj = c.get(i);
		Field[] fields = obj.getClass().getDeclaredFields();
		TreeMap<String, Object> fieldAndValues = null;
		if ((fields != null) && (fields.length > 0)) {
			fieldAndValues = new TreeMap<>(new Comparator<String>() {
				@Override
				public int compare(String o1, String o2) {
					return o1.compareToIgnoreCase(o2);
				}
			});
			String name = "";
			Object value = null;
			Field field = null;
			//Class clazz = null;
			for(int j = 0,jsize = fields.length;j < jsize; j ++) {
				field = fields[i];
				name = field.getName();
				if("serialversionuid".equalsIgnoreCase(name)) {
					continue;
				}
				value =	ReflectionUtil.getFieldValue(obj, name);
				//clazz = field.getType();
				if(null == value) {
					continue;
				}
				fieldAndValues.put(name, value);				
				
//				if(String.class.equals(clazz)) {//字符串
//					
//				} else if(Integer.class.equals(clazz)) {//整型
//					
//				} else if(Long.class.equals(clazz)) {//长整型
//					
//				} else if(Date.class.equals(clazz)) {//日期类型
//					
//				}
			}
		}

	}

	@Override
	public int getBatchSize() {
		// TODO Auto-generated method stub
		return 0;
	}
	
	public static void main(String[] args) {
		TreeMap<String, Object> result = new TreeMap<>(new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				// TODO Auto-generated method stub
				return o1.compareToIgnoreCase(o2);
			}
		});
		result.put("a", 1);
		result.put("c", 3);
		result.put("b", 2);
		System.out.println(result.toString());
	}

}
